package com.yzt.message.activity;

import android.content.Context;
import android.content.Intent;

import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSON;
import com.yzt.zhuju.base.BaseActivity;
import com.yzt.zhuju.network.entity.message.CompanyNoticeData;
import com.yzt.zhuju.widget.refreshHeader.WaterDropHeader;
import com.yzt.message.R;
import com.yzt.message.adapter.OfficeNoticeAdapter;
import com.yzt.message.custom.BroadcastMessageContent;
import com.scwang.smart.refresh.layout.api.RefreshLayout;

import java.util.ArrayList;
import java.util.List;

import io.rong.imlib.RongIMClient;
import io.rong.imlib.model.Conversation;
import io.rong.imlib.model.Message;

/**
 * author : Irene
 * e-mail : 821970751@qq.com
 * date   : 2020/6/2 1:40 PM
 * desc   :官方公告
 */
@Deprecated
public class OfficeNoticeActivity extends BaseActivity {

    private RefreshLayout mRefreshLayout;

    private OfficeNoticeAdapter mOfficeNoticeAdapter;

    public static void startActivity(Context mActivity) {

        mActivity.startActivity(new Intent(mActivity, OfficeNoticeActivity.class));
    }

    @Override


    public int getLayoutId() {
        return R.layout.activity_office_notice;
    }

    @Override
    public void installViews() {
        mTitleBar.setTitle(getString(R.string.office_notice));
        mRefreshLayout = findViewById(R.id.refreshLayout);
        mRefreshLayout.setRefreshHeader(new WaterDropHeader(mActivity));
        RecyclerView recyclerView = findViewById(R.id.recyclerView);
        LinearLayoutManager mLinearLayoutManager = new LinearLayoutManager(mActivity);
        mLinearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(mLinearLayoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());

        mOfficeNoticeAdapter = new OfficeNoticeAdapter();
        recyclerView.setAdapter(mOfficeNoticeAdapter);

        mRefreshLayout.setOnRefreshListener(refreshLayout ->
                getHistoryMessage()
        );
        getHistoryMessage();
    }

    private void getHistoryMessage() {

        RongIMClient.getInstance().getHistoryMessages(Conversation.ConversationType.SYSTEM, "11",
                -1, 1000, new RongIMClient.ResultCallback<List<Message>>() {
                    @Override
                    public void onSuccess(List<io.rong.imlib.model.Message> messages) {
                        loadData(messages);
                        mTitleBar.postDelayed(() ->
                                mRefreshLayout.closeHeaderOrFooter(), 1000
                        );
                    }

                    @Override
                    public void onError(RongIMClient.ErrorCode e) {
                        mRefreshLayout.closeHeaderOrFooter();
                    }
                });
    }

    private void loadData(List<io.rong.imlib.model.Message> messages) {

        if (messages == null || messages.size() == 0) {
            return;
        }
        ArrayList<CompanyNoticeData> mDatas = new ArrayList<>();
        for (int index = 0; index < messages.size(); index++) {
            io.rong.imlib.model.Message mMessage = messages.get(index);

            if (mMessage.getContent() instanceof BroadcastMessageContent) {
                BroadcastMessageContent mBroadcastMessageContent = (BroadcastMessageContent) (mMessage.getContent());
                String extra = mBroadcastMessageContent.getExtra();
                CompanyNoticeData mCompanyNoticeData = JSON.parseObject(extra, CompanyNoticeData.class);
                mCompanyNoticeData.setTime(mMessage.getReceivedTime());
                mDatas.add(mCompanyNoticeData);
            }
        }

        //清空上一次拉取的记录
        mOfficeNoticeAdapter.getData().clear();
        mOfficeNoticeAdapter.addData(mDatas);
    }
}
